package com.xckj.teacher.ui.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xckj.teacher.R;
import com.xckj.teacher.bean.HomeBean;
import com.xckj.teacher.util.LoadImageUtil;

import java.util.List;

/**
 * @Author: ChenYao
 * @Describe: 首页收藏列表
 * @Date: 2024/4/11 22:29
 */
public class HomeCollectionListAdapter extends RecyclerView.Adapter {
    private List<HomeBean.Collect> collects;
    private OnItemClickListener onItemClickListener;


    @SuppressLint("NotifyDataSetChanged")
    public void setCollectsData(List<HomeBean.Collect> collects) {
        this.collects = collects;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_collection_list, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.itemView.setOnClickListener(view -> {
            if (null != onItemClickListener) {
                onItemClickListener.onItemClick(view, position);
            }
        });
        HomeBean.Collect collect = collects.get(position);
        LoadImageUtil.loadImage(collect.cover_full, itemViewHolder.ivCover);
        itemViewHolder.tvTitle.setText(collect.gradeName + collect.productName);
    }

    @Override
    public int getItemCount() {
        return collects == null ? 0 : collects.size();
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private final ImageView ivCover;
        private final TextView tvTitle;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            ivCover = itemView.findViewById(R.id.iv_cover);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }
    }

    public interface OnItemClickListener {
        /**
         * Item的点击事件
         *
         * @param view     当前视图
         * @param position 当前索引
         */
        void onItemClick(View view, int position);
    }
}
